package com.example.hans.navigationbyviewpageandradiobutton;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


//ListView的适配器
public class ContactAdapterForListView extends ArrayAdapter<Contact> {

    private int resourceId;

    public ContactAdapterForListView(Context context,int textViewResourceId, List<Contact> contactList) {
        super(context,textViewResourceId,contactList);
        resourceId = textViewResourceId;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Contact contact = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = view.findViewById(R.id.head_portrait);
            viewHolder.textView = view.findViewById(R.id.tv_info);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.imageView.setImageResource(R.drawable.contactor);
        viewHolder.textView.setText(contact.getName()+"\n"+contact.getPhone());
        return view;
    }

    class ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
